def can_split(num_str, target):
    """
    回溯判断num_str能否分割为若干子串，其整数和为target
    """
    def backtrack(index, curr_sum):
        if index == len(num_str):
            return curr_sum == target
        for end in range(index + 1, len(num_str) + 1):
            part = int(num_str[index:end])
            if backtrack(end, curr_sum + part):
                return True
        return False
    return backtrack(0, 0)

def punishment_number(n):
    res = 0
    for i in range(1, n + 1):
        sq = i * i
        if can_split(str(sq), i):
            res += sq
    return res

if __name__ == "__main__":
    n = 37  # 可替换为任意n
    print(punishment_number(n))
